<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\PaymentRequest;
use Excel;
class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home.index');
    }

    public function getInfo()
    {
        $payments = PaymentRequest::orderBy('name','desc')->get();
        return $payments;
    }

    public function update(Request $request)
    {
        $pays = PaymentRequest::where('name',$request->name)->first();
        $pays->prices = $request->prices;
        $pays->save();

    }

    public function download(Request $request)
    {

     $data = PaymentRequest::whereIn('name',$request->selected)->get();
     $cellData = array();
     foreach($data as $k=>$v){
        $new = array();
        $new['编号']     = $k+1;
        $new['身份证']   = $v->id_number;
        $new['手机']     = $v->phone;
        $new['周期']     = $v->stage;
        $new['货款额度'] = $v->prices;
        $new['地址']     = $v->adress;
        $new['申请ip']   = $v->request_ip;
        $new['申请时间'] = $v->created_at;

        $cellData[] = $new;
     }
     
     Excel::create('货款申请', function($excel) use ($cellData){ 
       $excel->sheet('Sheetname', function($sheet) use ($cellData) {

        $sheet->setWidth(array(
            'A'     =>  10,
            'B'     =>  20,
            'C'     =>  20,
            'D'     =>  20,
            'E'     =>  20,
            'F'     =>  20,
            'G'     =>  20,
            'H'     =>  20,
        ));
 
       $sheet->fromArray($cellData); 

        });

      })->store('xls','exports');

      $path = array('path'  => 'http://'.$request->getHttpHost().'/exports/货款申请.xls');
      return $path;

    } 


}
